﻿using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Common.Logging;

namespace Wicresoft.Task.EmployeeSync
{
    public static class ADHelper
    {

        public static CTMPeopleModel ConvertFromDirectory(DirectoryEntry entry, ILog logger)
        {
            
            var model = new CTMPeopleModel();
            try
            {
                var properties = entry.Properties;

                model.RemedyLoginID = ((string)properties["cn"].Value).ToLower();
                model.EmployeeNumber = ((string)properties["cn"].Value).ToLower();
                model.PhoneNumberBusiness = properties["mobile"] != null ? ((string)properties["mobile"].Value).ToLower() : "####";
                model.FullName = (string)properties["displayName"].Value;
                model.InternetEmail = (string)properties["mail"].Value;
                model.Department = (string)properties["department"].Value;
                model.JobTitle = (string)properties["title"].Value;
                model.ProfileStatus = CTMPeopleModel.ProfileStatus_Enum.Enabled;
            }
            catch(Exception ex)
            {
                logger.Error("ADHelper Error:【"+model.EmployeeNumber+"】", ex);
            }

            return model;
        }

        public static string GetDateString(DateTime date)
        {
            return date.Year.ToString("D4") + date.Month.ToString("D2") + date.Day.ToString("D2") + date.Hour.ToString("D2") + date.Minute.ToString("D2") + date.Second.ToString("D2") + ".0Z";
        }
    }
}
